package com.example.recyclerview6;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import com.scwang.smartrefresh.header.BezierCircleHeader;
import com.scwang.smartrefresh.header.DeliveryHeader;
import com.scwang.smartrefresh.header.DropBoxHeader;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * 使用SmartRefreshLayout实现下拉刷新、上拉加载更多
 * 报错Cannot access ‘android.support.v4.view.NestedScrollingParent’ which is a supertype of ‘com.scwang.smart.refresh.layout.SmartRefreshLayout’. Check your module classpath for missing or conflicting dependencies
 * 在gradle.properties中加入android.enableJetifier=true
 */
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    private ProductListAdapter productListAdapter;

    private SmartRefreshLayout smartRefreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取RecyclerView
        recyclerView  = findViewById(R.id.recyclerView);

        // 设置RecyclerView的productListAdapter
        ArrayList<Product> products = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            products.add(new Product(Long.valueOf(i), "商品" + i, "风格" + i, i, new BigDecimal(i + ".99")));
        }
        productListAdapter = new ProductListAdapter(products);
        recyclerView.setAdapter(productListAdapter);

        // 获取SmartRefreshLayout
        smartRefreshLayout = findViewById(R.id.smartRefreshLayout);
        // 设置刷新头（需要导入依赖）为转圈圈
        smartRefreshLayout.setRefreshHeader(new MaterialHeader(this));
        // 下拉刷新
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                // 新增数据
                products.clear();
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                products.add(new Product(-1L, "商品", "风格", 432, new BigDecimal("-1.99")));
                // 刷新RecyclerView
                productListAdapter.notifyDataSetChanged();
                // 完成刷新（不然会一直刷新不会停，不停就不能再次刷新，而且有个东西在转
                smartRefreshLayout.finishRefresh(true);
            }
        });
        // 上拉加载更多
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                // 新增数据
                products.add(products.size(), new Product(999L, "商品", "风格", 432, new BigDecimal("999.99")));
                products.add(products.size(), new Product(999L, "商品", "风格", 432, new BigDecimal("999.99")));
                products.add(products.size(), new Product(999L, "商品", "风格", 432, new BigDecimal("999.99")));
                products.add(products.size(), new Product(999L, "商品", "风格", 432, new BigDecimal("999.99")));
                products.add(products.size(), new Product(999L, "商品", "风格", 432, new BigDecimal("999.99")));
                products.add(products.size(), new Product(999L, "商品", "风格", 432, new BigDecimal("999.99")));
                products.add(products.size(), new Product(999L, "商品", "风格", 432, new BigDecimal("999.99")));
                // 刷新RecyclerView
                productListAdapter.notifyDataSetChanged();
                // 完成刷新（不然会一直刷新不会停，不停就不能再次刷新，而且有个东西在转
                smartRefreshLayout.finishLoadMore(true);
            }
        });
    }
}